from abc import ABC, abstractmethod

class Product(ABC):
    def __init__(self, product_id, name, price, description, stock, seller_id, image_path=None, name_en=None, description_en=None):
        self.product_id = product_id
        self.name = name
        self.name_en = name_en or name  # Default to name if name_en not provided
        self._price = price
        self.description = description
        self.description_en = description_en or description  # Default to description if description_en not provided
        self.stock = stock
        self.seller_id = seller_id
        self.image_path = image_path
    
    def get_display_name(self):
        """获取显示名称 - 根据当前语言返回中文或英文"""
        from core.utils.i18n import get_current_language
        current_lang = get_current_language()
        
        # 如果是中文，返回中文名称；否则返回英文名称
        if current_lang == "zh_CN":
            return self.name
        else:
            return self.name_en
    
    @abstractmethod
    def get_category(self):
        pass
    
    @abstractmethod
    def calculate_discount(self):
        """
        计算折扣金额（多态方法）
        每个商品子类需要实现自己的折扣策略
        
        Returns:
            float: 折扣金额
        """
        pass
    
    @property
    def price(self):
        return self._price
    
    @price.setter
    def price(self, value):
        if value < 0:
            raise ValueError("Price cannot be negative")
        self._price = value
    
    def get_discount_rate(self):
        """
        获取折扣率
        
        Returns:
            float: 折扣率（0-1之间的值）
        """
        discount_amount = self.calculate_discount()
        if self._price == 0:
            return 0.0
        return discount_amount / self._price
    
    def get_discounted_price(self):
        """
        获取折扣后的价格
        
        Returns:
            float: 折扣后的价格
        """
        discount_amount = self.calculate_discount()
        return max(0, self._price - discount_amount)
    
    def to_dict(self):
        return {
            'product_id': self.product_id,
            'name': self.name,
            'price': self._price,
            'description': self.description,
            'stock': self.stock,
            'seller_id': self.seller_id,
            'category': self.get_category(),
            'image_path': self.image_path,
            'discount_rate': self.get_discount_rate(),
            'discounted_price': self.get_discounted_price()
        }

class Electronics(Product):
    def get_category(self):
        return "electronics"
    
    def calculate_discount(self):
        """电子产品10%折扣"""
        return self._price * 0.10
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand, warranty, image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.warranty = warranty

class Clothing(Product):
    def get_category(self):
        return "clothing"
    
    def calculate_discount(self):
        """服装15%折扣（季节性促销）"""
        return self._price * 0.15
    
    def __init__(self, product_id, name, price, description, stock, seller_id, size, color, image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.size = size
        self.color = color

class Books(Product):
    def get_category(self):
        return "books"
    
    def calculate_discount(self):
        """图书5%折扣（固定折扣）"""
        return self._price * 0.05
    
    def __init__(self, product_id, name, price, description, stock, seller_id, author, isbn, image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.author = author
        self.isbn = isbn

class Home(Product):
    def get_category(self):
        return "home"
    
    def calculate_discount(self):
        """家居用品8%折扣"""
        return self._price * 0.08
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand="", material="", image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.material = material

class Beauty(Product):
    def get_category(self):
        return "beauty"
    
    def calculate_discount(self):
        """美妆产品12%折扣"""
        return self._price * 0.12
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand="", origin="", image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.origin = origin

class Food(Product):
    def get_category(self):
        return "food"
    
    def calculate_discount(self):
        """食品3%折扣（小幅折扣）"""
        return self._price * 0.03
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand="", expiry_date="", image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.expiry_date = expiry_date

class Sports(Product):
    def get_category(self):
        return "sports"
    
    def calculate_discount(self):
        """运动用品10%折扣"""
        return self._price * 0.10
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand="", weight="", image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.weight = weight

class Accessories(Product):
    def get_category(self):
        return "accessories"
    
    def calculate_discount(self):
        """配件20%折扣（高折扣促销）"""
        return self._price * 0.20
    
    def __init__(self, product_id, name, price, description, stock, seller_id, brand="", compatible="", image_path=None, name_en=None, description_en=None):
        super().__init__(product_id, name, price, description, stock, seller_id, image_path, name_en, description_en)
        self.brand = brand
        self.compatible = compatible